﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;

namespace System.IO
{
    public static class StreamEx
    {
        /// <summary>
        /// A kapott tiff Stream-et 
        /// png Stream-é alakítja
        /// </summary>
        /// <param name="tiffStream"></param>
        /// <returns></returns>
        public static Stream TiffStreamToPngStream(this Stream tiffStream)
        {
            try
            {
                MemoryStream pngStream = new MemoryStream();
                if (tiffStream.Length > 0)
                {
                    new Bitmap(tiffStream).Save(pngStream, ImageFormat.Png);
                }
                return pngStream;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        /// <summary>
        /// A kapott Stream-et
        /// byte tömbbé alakítja
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static byte[] ToByteArray(this Stream stream)
        {
            stream.Position = 0;
            byte[] buffer = new byte[stream.Length];
            for (int totalBytesCopied = 0; totalBytesCopied < stream.Length; )
                totalBytesCopied += stream.Read(buffer, totalBytesCopied, Convert.ToInt32(stream.Length) - totalBytesCopied);
            return buffer;
        }
    }
}
